package info.techgeek.labs.regex.dbuscenario1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParamSubstitution {

	public static void main(String args[]){

		StringBuffer query = new StringBuffer("");
		query.append("SELECT LG.OP_AA_CODE, LOT.LOT_DESCRIPTION, LG.OP_OTHER_INFO,LOT.LOT_XML_FORMAT ");
		query.append("FROM DBUI_SC_LG_OPERATION LG,DBUI_SC_MA_LOG_OP_TYPE LOT WHERE LOT.LOT_CODE = LG.OP_LOT_CODE AND LG.OP_LOT_NUM = &LOT "); 
		query.append("AND LG.OP_US_CODE = &NOME AND TRUNC(LG.OP_DATE) > TRUNC(TO_DATE(&LOT_DATE)) AND LOT.LOT_CODE = LG.OP_LOT_CODE ");
		query.append("AND LG.OP_LOT_CODE = &LOT_CODE ");
		/*
		StringBuffer query = new StringBuffer("");
		query.append("AND LG.OP_ &LOT  LOT_CODE = &LOT_CODE &LOT_DATE LOT_CODE = &LOT_NUM  ");		
		 */
		
		System.out.println(" old Query "+query);
		
		ParamSubstitution paramSubstitution = new ParamSubstitution();
		String newQuery = (String)paramSubstitution.getValidationParamsMap(query.toString(), "1");
	
		System.out.println(" new Query "+newQuery);
		/*
		List<String>  list = paramSubstitution.getQueryParamsList(query.toString());
		for(String string : list ){
			System.out.println(" string "+string);
		}
		*/
	}
	
	
	private String getValidationParamsMap(String query,String interrogationCode){
		System.out.println("Begin getValidationParamsMap "+query);
		
		Map<String,String> validationParamMap = new HashMap<String, String>();
		String paramDummyValue = "0";
		int paramType = 0;
		String [] arr;
		List<String> paramList = (ArrayList<String>)getQueryParamsList(query);
		
		validationParamMap.put("LOT", "11");
		validationParamMap.put("NOME", "SS");
		validationParamMap.put("LOT_DATE", "DD");
		validationParamMap.put("LOT_CODE", "GG");
		 
		 
		for(String param : paramList){
			
					
					/*switch (paramType) {
	                 case 2:   //Alfanumerico
	                	 paramDummyValue="'"+paramDummyValue+"'";
	                      break;
	                 case 4:   //Data in , Dec. out
	                	 paramDummyValue= paramDummyValue.replaceAll("/","");
	                      break;
	                 case 6:   //Lista alfanumerici
	                      arr=paramDummyValue.split(",");
	                      paramDummyValue="";
	                      for (int i=0; i<= arr.length-1; i++) {
	                          if (!arr[i].equals("") && !arr[i].equals(null)){
	                        	  paramDummyValue+="'"+arr[i]+"',";
	                          }
	                      }
	                      paramDummyValue=paramDummyValue.substring(0,paramDummyValue.length()-1);
	                 break;
	                 case 8:   //Lista date (Data in, Dec. out)
	                	 paramDummyValue= paramDummyValue.replaceAll("/","");
	                      break;
					}*/
//				validationParamMap.put(param,paramDummyValue);
				query = replaceParamWithValues1(query, param, paramDummyValue);
//				query = query.replace("&"+param, paramDummyValue);
		}
		
		System.out.println("End getValidationParamsMap "+query);
		return query;
	}
	
	
	private List<String> getQueryParamsList(String query)
	{
		Pattern p = Pattern.compile("&[a-z0-9_\\-A-Z]+");
		Matcher m = p.matcher(query);
		List<String> aList = new ArrayList<String>(); 
		String str ;
		while (m.find())
		{
			str = m.group();
			str = str.substring(1);
			if(!aList.contains(str)){
				System.out.println(" str "+str);
				aList.add(str);
			}
		}
//		System.out.println(" alist "+aList);
//		System.out.println(" alist Size "+aList.size());
		return aList;
	}
	
	
	private String replaceParamWithValues(String query,String param,String paramDummyValue){
		
		System.out.println(" before Compile "+"&"+param+"[a-z0-9_\\-A-Z]?");
		Pattern p = Pattern.compile("&"+param+"?=a-z0-9_\\-A-Z");
//		Pattern p = Pattern.compile("&[a-z0-9_\\-A-Z]+");
		Matcher m = p.matcher(query);
		List<String> aList = new ArrayList<String>(); 
		String str ;
		while (m.find())
		{
			str = m.group();
			str = str.substring(1);
			System.out.println("str "+str);
			if(!aList.contains(str))
				aList.add(str);
		}
		if(aList.size() > 0){
			for(String chkParams : aList){
				System.out.println("chkParams "+chkParams);
				System.out.println("param "+param);
				if(chkParams.equals(param)){
					System.out.println("They are Equal ");
					query = query.replace("&"+param, paramDummyValue);
				}
				
			}
			
		}else{
			System.out.println("Inside Else Part "+param);
			query = query.replace("&"+param, paramDummyValue);
		}
		System.out.println(" query "+query);
		System.out.println(" alist Size "+aList.size());
		return query;
	}
	
	/*private String getQuerySubstitutedWithParamValues(String query,List<String> paramList,String interrogationCode,Map<String,String> validationParamsMap) throws NDWDaoException {
		System.out.println("Begin getQuerySubstitutedWithParamValues ");
		
		String paramDummyValue;
		int paramType = 0;
		String [] arr;
		for(String param : paramList){
			
			List<IiParam> iiParamList = (ArrayList<IiParam>)paramsDao.getInterrogationParams(interrogationCode,param);
			
			if(iiParamList.size()>0){
				IiParam iiParam =(IiParam)iiParamList.get(0);
//				paramDummyValue = PropertyFileReader.getInstance().getParamProperty(iiParam.getParamType().toString());
				paramType = iiParam.getParamType().intValue();
				
				paramDummyValue = validationParamsMap.get(param);
				System.out.println("paramDummyValue "+paramDummyValue);
				switch (paramType) {
                 case 2:   //Alfanumerico
                	 paramDummyValue="'"+paramDummyValue+"'";
                      break;
                 case 4:   //Data in , Dec. out
                	 paramDummyValue= paramDummyValue.replaceAll("/","");
                      break;
                 case 6:   //Lista alfanumerici
                	  if("".equals(paramDummyValue) || !paramDummyValue.contains(",") ){
                		  paramDummyValue="'"+paramDummyValue+"'";
                	  }else{
                		  arr=paramDummyValue.split(",");
                		  paramDummyValue="";
                		  for (int i=0; i<= arr.length-1; i++) {
                			  if (!arr[i].equals("") && !arr[i].equals(null)){
                				  paramDummyValue+="'"+arr[i]+"',";
                			  }
                		  }
                		  paramDummyValue=paramDummyValue.substring(0,paramDummyValue.length()-1);
                	  }
                 break;
                 case 8:   //Lista date (Data in, Dec. out)
                	 paramDummyValue= paramDummyValue.replaceAll("/","");
                      break;
				}
//				query = query.replace("&"+param, paramDummyValue);
				query = replaceParamWithValues(query, param, paramDummyValue);
			}
		}
		System.out.println("End getQuerySubstitutedWithParamValues ");
		return query;
	}*/
	
private String replaceParamWithValues1(String query,String param,String paramDummyValue){
		
		System.out.println(" before Compile "+"b&"+param+"b");
		Pattern p = Pattern.compile("&"+param+"\\b");
//		Pattern p = Pattern.compile("&"+param+"?!a-z0-9_\\-A-Z");
		Matcher m = p.matcher(query);
		String query1 = "";
		query1 = m.replaceAll(paramDummyValue);
		System.out.println(" query1 "+query1);
		
//		List<String> aList = new ArrayList<String>(); 
//		String str ;
//		while (m.find())
//		{
//			str = m.group();
//			str = str.substring(1);
//			System.out.println("replacing "+m.replaceAll(paramDummyValue));
//			System.out.println("str "+str);
//			if(!aList.contains(str))
//				aList.add(str);
//		}

		System.out.println(" query "+query);
//		System.out.println(" alist Size "+aList.size());
		return query1;
	}
	
}
